<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Models\Gameshop;
class StudentController extends Controller
{
    public function index(){
        // 获取models 中 Gameshop 中的表数据存入$gameshops
        $gameshops = Gameshop::get();
        return view('gameshop/middle',['gameshops' => $gameshops]);
        // $bg_img = DB::table('bg_img')->get();
        // var_dump($bg_img);
    }
    public function login(){
        return view('gameshop/login');
    }
    public function register(){
        return view('gameshop/register');
    }
    public function addGame(Request $request){
        if($request->isMethod('POST')){
    		$this->validate($request,[
    			'gameshop.name' =>'required|min:1|max:10',  //必填，最小长度1，最大长度6
    			'gameshop.jiage' =>'required',		//必填，数字
    			'gameshop.one' =>'required|integer',
    			'gameshop.mm' =>'required',
    			'gameshop.qz' =>'required',
    			'gameshop.ms' =>'required|min:1|max:50',
    			'gameshop.sj' =>'required',	
    			'gameshop.file' =>'required',
    		],[
    			'required'=>':attribute 为必填项',
    			'min' => ':attribute 长度不能小于1个字符',
    			'max' => ':attribute 长度不能大于10个字符',
    			'integer' => ':attribute 必须为数字',
    		],[
    			'gameshop.name' => '游戏名称',
    			'gameshop.jiage' => '游戏价格',
    			'gameshop.one' => '游戏评分',
    			'gameshop.mm' => '游戏大小',
    			'gameshop.qz' => '游戏平台',
    			'gameshop.ms' => '游戏描述',
    			'gameshop.sj' => '游戏日期',
    			'gameshop.file' => '游戏图片',
    		]);

    		
    		$data = $request->input('gameshop');

    		$a = $request->input('my');
    		$b = $request->input('mye');
    		$my = '';
    		$mye = '';
    		foreach ($a as  $value) {						//循环获取游戏类型的值
				$my .=  "—".$value;
   			}
   			foreach ($b as  $value) {						//循环获取游戏类型的值
				$mye .=  "—".$value;
   			}

   			$arr = array('my' => $my,'mye'=>$mye);
   			$data = $data+$arr;

    		//var_dump($data);

    		$ret = gameshop::insert($data);
            if($ret){
    			return redirect('/')->with('error','添加成功！')->withInput();
    		}else{
    			return redirect('gameshop/addgame')->with('error','添加失败！')->withInput();
    		}
        }
        return view('gameshop/addgame');
    }
    public function Message(Request $request,$name){
        $gameshops = DB::select('select * from bg_img where name = '.$name);
        $gameshops = $gameshops[0];
        return view('other/gameMessage',['gameshops' => $gameshops]);
    }
	public function Delete($name){
		$ret = gameshop::where('name',$name)->delete();
        return redirect('/');
	}
}
